/**
 * @author ming
 * @date 2021/2/14 21:17
 * @desc mysql 连接信息
 */
package mysql

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

const (
	defaultDbAlias = "default"
)

// 连接mysql数据库
func init() {
	dbHost := beego.AppConfig.String("mysql-host")
	dbPort := beego.AppConfig.String("mysql-port")
	dbUser := beego.AppConfig.String("mysql-user")
	dbPassword := beego.AppConfig.String("mysql-password")
	dbName := beego.AppConfig.String("mysql-db-name")
	if dbPort == "" {
		dbPort = "3306"
	}

	connectStr := dbUser + ":" + dbPassword + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName + "?charset=utf8&&loc=Local"
	orm.RegisterDataBase(defaultDbAlias, "mysql", connectStr)

	idleConn := beego.AppConfig.DefaultInt("mysql-idle-conns", 4)
	maxConn := beego.AppConfig.DefaultInt("mysql-max-conns", 32)

	orm.SetMaxIdleConns(defaultDbAlias, idleConn)
	orm.SetMaxOpenConns(defaultDbAlias, maxConn)
}
